﻿using System.IO;
using System.Reflection;
using System.Windows;
using System.Windows.Controls;
using System.Xml;
using IDesktop.Core;

namespace IDesktop.View
{
    /// <summary>
    /// SelectCity.xaml 的交互逻辑
    /// </summary>
    public partial class SelectCity : UserControl
    {
        private static readonly DependencyProperty PathProperty = DependencyProperty.Register("Path", typeof(string), typeof(SelectCity));
        private XmlNode _rootNode;
        private XmlNode _provinceNode;
        private XmlNode _cityNode;
        private XmlNode _countryNode;

        public string Path {
            get {
                return (string)this.GetValue(PathProperty);
            }
            set {
                if (!string.IsNullOrEmpty(value)) {
                    this.SetValue(PathProperty, value);
                }
            }
        }

        public delegate void CityChangedEventHandler(object sender, CityChangedEventArgs e);

        public event CityChangedEventHandler CityChangedEvent;

        public SelectCity() {
            InitializeComponent();
        }

        private void Province_SelectionChanged(object sender, SelectionChangedEventArgs e) {
            city.Items.Clear();
            if (province.SelectedItem != null) {
                string provinceName = province.SelectedValue.ToString();

                foreach (XmlNode subNode in _rootNode.ChildNodes) {
                    if (subNode.Attributes["Name"].InnerText == provinceName) {
                        _provinceNode = subNode;
                    }
                }

                foreach (XmlNode subNode in _provinceNode.ChildNodes) {
                    city.Items.Add(subNode.Attributes["Name"]);
                }
            }
        }

        private void City_SelectionChanged(object sender, SelectionChangedEventArgs e) {
            country.Items.Clear();
            if (city.SelectedItem != null) {
                string cityName = city.SelectedValue.ToString();

                foreach (XmlNode subNode in _provinceNode.ChildNodes) {
                    if (subNode.Attributes["Name"].InnerText == cityName) {
                        _cityNode = subNode;
                    }
                }
                foreach (XmlNode subNode in _cityNode.ChildNodes) {
                    country.Items.Add(subNode.Attributes["Name"]);
                }
            }
        }

        private void SelectionChanged() {
            string id = _countryNode.FirstChild.Attributes["Name"].InnerText;
            string countryName = country.SelectedValue.ToString();
            CityChangedEventArgs args = new CityChangedEventArgs(id, countryName);
            if (CityChangedEvent != null) {
                CityChangedEvent(this, args);
            }
        }

        private void Country_SelectionChanged(object sender, SelectionChangedEventArgs e) {
            string countryName = country.SelectedValue.ToString();
            string id = string.Empty;
            foreach (XmlNode subNode in _cityNode.ChildNodes) {
                if (subNode.Attributes["Name"].InnerText == countryName) {
                    _countryNode = subNode;
                }
            }
            SelectionChanged();
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e) {
            if (Path != null) {
                Assembly assembly = Assembly.GetExecutingAssembly();
                Stream stream = assembly.GetManifestResourceStream("IDesktop.Resource.Citys.xml");
                if (stream != null) {
                    XmlDocument document = new XmlDocument();
                    document.Load(stream);
                    XmlNode xmlNode = document.SelectSingleNode("Citys");
                    _rootNode = xmlNode;
                    foreach (XmlNode subNode in xmlNode.ChildNodes) {
                        province.Items.Add(subNode.Attributes["Name"]);
                    }
                }
            }
        }
    }
}
